package web_k;

import mapper_k.HomeUserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo_k.HomeUser;
import service_k.HomeUserService;
import util_k.SqlSessionFactoryUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/homeUserRegisterServlet")
public class HomeUserRegisterServlet extends HttpServlet {

    private HomeUserService service = new HomeUserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受注册信息
        String homeuserName = request.getParameter("homeuserName");
        String homeuserPassword = request.getParameter("homeuserPassword");



        //封装用户对象
        HomeUser homeUser = new HomeUser();
        homeUser.setHomeuserName(homeuserName);
        homeUser.setHomeuserPassword(homeuserPassword);


        //先进行查询
        HomeUser homeUser1 = service.homeUserSelectByName(homeuserName);
        //然后进行判断
        if (homeUser1 == null){
            //该用户名可用
            service.homeUserAdd(homeUser);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("注册成功！");
        } else {
            //用户名已存在，提示用户重新输入
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("用户名已存在！");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
